package com.yanqu.road.server.cmd.pageant;


import com.yanqu.road.entity.activity.game.result.RankingDetailsResult;
import com.yanqu.road.pb.pageant.PageantProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.game2048.PageantModule;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

@Cmd(code = Protocol.S_PAGEANT_RANKING_DETAIL, desc = "排名详情")
public class PageantRankingDetailsCmd extends AbstractCommand {

    private static Logger logger = LogManager.getLogger(PageantRankingDetailsCmd.class.getName());

    private int activityId;

    private RankingDetailsResult result;

    private boolean neeSend = true;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        PageantProto.PageantRankingDetailsReqMsg reqMsg = PageantProto.PageantRankingDetailsReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if (activityId == 0) {
            ret = GameErrorCode.E_PAGEANT_ACTIVITY_EMPTY;
        }
        result = player.getModule(PageantModule.class).getRankingDetails(activityId);
        if (result.isNeedCross()) {
            //需要去跨服取
            neeSend = false;
            PageantProto.CrossPageantDetailReqMsg.Builder req = PageantProto.CrossPageantDetailReqMsg.newBuilder();
            req.setActivityId(activityId);
            player.sendPacket(CrossProtocol.C2_PAGEANT_DETAIL, req);
        }
    }


    @Override
    public void sendData(GamePlayer player) {
        if (neeSend) {
            PageantProto.PageantRankingDetailsRespMsg.Builder respMsg = PageantProto.PageantRankingDetailsRespMsg.newBuilder();
            if(0==ret&&0 == result.getRet()) {
                respMsg.setBeautyId(result.getBeautyId());
                respMsg.setTotalScore(result.getTotalScore());
                respMsg.setTotalRanking(result.getTotalRanking());
                respMsg.setNextTotalRanking(result.getNextTotalRanking());
                respMsg.setRecordScore(result.getRecordScore());
                respMsg.setRecordRanking(result.getRecordRanking());
                respMsg.setNextRecordRanking(result.getNextRecordRanking());
            } else {
                respMsg.setBeautyId(0);
                respMsg.setTotalScore(0);
                respMsg.setTotalRanking(0);
                respMsg.setNextTotalRanking(0);
                respMsg.setRecordScore(0);
                respMsg.setRecordRanking(0);
                respMsg.setNextRecordRanking(0);
            }
            player.sendPacket(Protocol.U_PAGEANT_RANKING_DETAIL, respMsg);
        }
    }
}
